/**
 * TtColLogAction.java
 * Created at 2017-3-20
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.RmsUtil;
import com.broadtext.rms.system.model.AccessLog;
import com.broadtext.rms.system.service.ITtColLogService;
import com.broadtext.rms.system.vo.LogExportModel;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: TtColLogAction</p>
 * <p>Description: 采集日志信息控制层</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-3-20</p>
 */
@Controller
@RequestMapping("/main/syslog")
public class TtColLogAction {
    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(TtColLogAction.class);

    /**
     * 区域服务类
     */
    @Autowired
    @Qualifier("ttColLogService")
    private ITtColLogService ttColLogService;
    
    
    /**
     * <p>
     * Description: 查询区域信息
     * </p>
     * 
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping(value = "/getLogs.mvc")
    @ResponseBody
    public void getLogs(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator<?> it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if(obj.get("value")!=null && !"".equals(obj.get("value")+"")){
                        map.put(obj.get("name")+"", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit()+1);
            EntityPageBean<AccessLog> pageBean = this.ttColLogService.findEntityPageByQuery(map, pageBounds);
            Map<String, Object> dataMap=new HashMap<String, Object>();
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.write(JSON.toJSONString(dataMap));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 
     * <p>Description: 导出日志 然后加入相关的日志记录，最后来导出</p>
     * @param request 请求
     * @param response 响应
     * @throws IOException 异常
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/exportLog.mvc")
    public void exportLog(HttpServletRequest request, HttpServletResponse response) throws IOException, DaoException{
        List<LogExportModel> logExport = new ArrayList<LogExportModel>();
        logExport = this.ttColLogService.findExportLogList();
        //导出
        ExportParams param;
        //标题 sheet名字
        param = new ExportParams("导出日志", "sheet");
        //excel类型 07或者03 不推荐07慢
        param.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(param, LogExportModel.class, logExport);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        workbook.write(bao);
        RmsUtil.exportExcel("日志导出.xlsx", response, bao);
        bao.close();
    }
}
